package com.wf.mock.app.mina.app;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.wf.mock.app.mina.ConnectorManager;
import com.wf.mock.app.mina.MessageIntentType;
import com.wf.mock.app.mina.RetrySendMsgManager;
import com.wf.mock.app.mina.dto.RawMessageDTO;
import com.wf.mock.app.utils.MinaUtils;
import com.wf.mock.app.utils.TokenUtils;

public class Mock20Client {

	public final static String[] users = { "guojh", "renweiguo", "zhangzhen", "wangxiaolong", "qiaolibo", "lirong",
			"yichangan", "hanwu", "guodan", "baixiaolong"};// , "fengxiang",

	/**
	 * 用户名缓存
	 */
	public final static Set<String> userSet = Sets.newHashSet(users);

	public final static Map<String, IoSession> userSessionMap = Maps.newHashMap();

	public static void main(String[] args) throws InterruptedException {
		initUserSessions();

		ExecutorService service = Executors.newFixedThreadPool(20);

		for (final String userId : userSet) {
			if ("guochao".equals(userId)) {
				continue;
			}
			service.submit(new Runnable() {

				public void run() {
					IoSession ioSession = userSessionMap.get(userId);

					for (int i = 0; i < 10; i++) {
//						for (String target : userSet) {
//							if (!userId.equals(target)) {
								String msgObj = buildApp2PortalPrivateMsg(userId, "guochao");

								RawMessageDTO rawMessageDTO = RawMessageDTO.create(msgObj);
								MinaUtils.inject2ContentTime(rawMessageDTO.getMsgBody());
								ioSession.write(rawMessageDTO.toString());

								RetrySendMsgManager.INSTANCE.addEvent(rawMessageDTO, ioSession);
								sleep();
//							}
//						}

					}

				}

				/**
				 * 
				 */
				private void sleep() {
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		}

		while (true) {
			TimeUnit.SECONDS.sleep(30);
		}
	}

	/**
	 * 初始化用户连接
	 */
	private static void initUserSessions() {
		IoConnector connector = ConnectorManager.getAppConnector();

		for (String userId : userSet) {
			ConnectFuture connectFuture = connector.connect();

			connectFuture.awaitUninterruptibly();

			IoSession session = connectFuture.getSession();
			session.setAttribute("userId", userId);

			session.write(MessageIntentType.APPCONNECT + "{'token':'"
					+ TokenUtils.generateToken(userId + "@wafertest.com") + "','markId':'" + MinaUtils.generateUUID()
					+ "'}");
			userSessionMap.put(userId, session);

		}

	}

	private static String buildApp2PortalPrivateMsg(String userId, String target) {
		long sendTime = System.currentTimeMillis();
		String uuid = MinaUtils.generateUUID();
		return MessageIntentType.APP2PORTAL + "{'userId':'" + userId
				+ "','domain':'wafertest.com','operateType':'SENDMSG','markId':'" + uuid + "','content':'" + sendTime
				+ "','timeStamp':" + sendTime + ",'unreadSize':0,'msgType':10,'contentType':'TEXT_PLAIN','target':'"
				+ target + "'}";
	}
}
